package de.tewdreyer.android.quaffed;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;

public class GetMeal extends Activity {
    private static final int DATE_DIALOG_ID = 0;
    private static final int TIME_DIALOG_ID = 1;

    Button mDate;
    Button mTime;
    EditText mVolume;

    private int mYear;
    private int mMonth;
    private int mDay;

    private int mHour;
    private int mMinute;

    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view,int year,int monthOfYear,int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;

                    mDate.setText(String.format("%02d.%02d.%d",mDay,mMonth+1,mYear));
                }
            };

    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view,int hourOfDay,int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;

                    mTime.setText(String.format("%02d:%02d",mHour,mMinute));
                }
            };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.get_meal);

        mDate = (Button)findViewById(R.id.btnDate);
        mDate.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                showDialog(DATE_DIALOG_ID);
            }
        });

        mTime = (Button)findViewById(R.id.btnTime);
        mTime.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                showDialog(TIME_DIALOG_ID);
            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mHour = c.get(Calendar.HOUR_OF_DAY);
        mMinute = c.get(Calendar.MINUTE);
    }

    // TODO why getParent() and not getApplicationContext()?
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(getParent(),mDateSetListener,mYear, mMonth, mDay);
            case TIME_DIALOG_ID:
                return new TimePickerDialog(getParent(),mTimeSetListener,mHour,mMinute,true);
        }
        return null;
    }
}